package com.yaoxun.console.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yaoxun.console.code.BaseResponseCode;
import com.yaoxun.console.service.RouterService;
import com.yaoxun.console.xo.req.RouterDeleteReq;
import com.yaoxun.console.xo.req.RouterEditReq;
import com.yaoxun.console.xo.req.RouterIdReq;
import com.yaoxun.console.xo.req.RouterListReq;
import com.yaoxun.console.xo.resp.ApiResp;
import com.yaoxun.console.xo.resp.BooResp;
import com.yaoxun.console.xo.resp.PageInfo;
import com.yaoxun.console.xo.resp.RouterResp;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/router")
public class RouterController implements BaseController {

	@Autowired
	private RouterService routerService;
	
	@GetMapping("/index.html")
	public String index() {
		return "sys/router/router-index";
	}
	
	@GetMapping("/{namespace}/add.html")
	public ModelAndView add(@PathVariable String namespace) {
		ModelAndView mv = new ModelAndView("sys/router/router-edit");
		RouterResp router = new RouterResp();
		router.setNamespace(namespace);
		mv.addObject("obj", router);
		return mv;
	}
	
	@GetMapping("/update/{namespace}/{id}.html")
	public ModelAndView update(@PathVariable String namespace, @PathVariable String id) {
		ModelAndView mv = new ModelAndView("sys/router/router-edit");
		RouterResp router = new RouterResp();
		router.setId(id)
			  .setNamespace(namespace);
		mv.addObject("obj", router);
		return mv;
	}
	
	@GetMapping("/detail/{namespace}/{id}.html")
	public ModelAndView detail(@PathVariable String namespace, @PathVariable String id) {
		ModelAndView mv = new ModelAndView("sys/router/router-detail");		
		RouterResp router = new RouterResp();
		router.setId(id)
		.setNamespace(namespace);
		mv.addObject("obj", router);
		return mv;
	}
	
	
	@ResponseBody
	@PostMapping("/get")
	public ApiResp<RouterResp> get(@Validated @RequestBody RouterIdReq req) {
		RouterResp resp = routerService.get(req);
		return ofRo(BaseResponseCode.SEARCH_SUCCESS, resp);
	}
	
	@ResponseBody
	@PostMapping("/add")
	public ApiResp<BooResp> add(@Validated @RequestBody RouterEditReq req) {
		
		log.info("router add req:{}", req);
		
		routerService.add(req);
		
		return ofRo(BaseResponseCode.ADD_SUCCESS, BooResp.of(true));
	}
	
	@ResponseBody
	@PostMapping("/update")
	public ApiResp<BooResp> update(@Validated @RequestBody RouterEditReq req) {
		
		log.info("router update req:{}", req);
		
		routerService.update(req);
		
		return ofRo(BaseResponseCode.UPDATE_SUCCESS, BooResp.of(true));
		
	}
	
	@ResponseBody
	@PostMapping("/delete")
	public ApiResp<BooResp> delete(@Validated @RequestBody RouterDeleteReq req) {
		log.info("router delete req:{}", req);
		
		routerService.delete(req);
		
		return ofRo(BaseResponseCode.DELETE_SUCCESS, BooResp.of(true));
	}
	
	@ResponseBody
	@PostMapping("/list")
	public ApiResp<PageInfo<RouterResp>> list(@Validated @RequestBody RouterListReq req) {
		
		PageInfo<RouterResp> pageInfo = routerService.list(req);
		
		return ofRo(BaseResponseCode.SEARCH_SUCCESS, pageInfo);
		
	}
	
}
